# Replication Archive for: Schwarz, Susanne and Alexander Coppock. (2020) 
# "What Have We Learned About Gender From Candidate Choice Experiments? 
# A Meta-analysis of 67 Factorial Survey Experiments" 
# Forthcoming, Journal of Politics.

rm(list = ls())
library(tidyverse)
library(estimatr)
library(rmeta)
library(xtable)
library(reshape2)
library(janitor)
source("helpers.R")

# Figure 2 ----------------------------------------------------------------

cand_race_cate_estimates <- read_rds("cand_race_cate_estimates.rds")

summary_df <-
  cand_race_cate_estimates %>%
  group_by(cand_white) %>%
  do(tidy(with(., meta.summaries(estimate, std.error, method = "random"))))

# is the difference-in-cates significant?
summary_df %>%
  melt(id.vars = c("cand_white")) %>%
  dcast(. ~ cand_white + variable) %>%
  clean_names() %>%
  transmute(
    estimate = white_candidate_estimate - black_candidate_estimate,
    std.error = sqrt(white_candidate_std_error ^ 2 + black_candidate_std_error ^ 2),
    statistic = estimate / std.error,
    p.value = 2 * pnorm(abs(statistic), lower.tail = FALSE)
  )

summary_df <-
  summary_df %>%
  mutate(
    study_name_short = "Random-effects meta-analysis",
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100
  )

gg_df <-
  cand_race_cate_estimates %>%
  bind_rows(summary_df) %>%
  mutate(
    facet = as.numeric(study_name_short == "Random-effects meta-analysis"),
    estimate_100 = estimate*100,
    std.error_100 = std.error*100,
    conf.high_100 = conf.high*100,
    conf.low_100 = conf.low*100,
    se_entry = make_se_entry(estimate_100, std.error_100, digits = 1))

difference_df <-
  gg_df %>%
  ungroup %>%
  select(study_name_short, cand_white, estimate) %>%
  melt(id.vars = c("study_name_short", "cand_white")) %>%
  dcast(study_name_short ~ cand_white + variable) %>%
  mutate(
    difference = `White candidate_estimate` - `Black candidate_estimate`,
    study_name_short_fac = fct_reorder(study_name_short, difference)
  )

gg_df <- 
  gg_df %>%
  mutate(study_name_short = factor(study_name_short, levels = levels(difference_df$study_name_short_fac)))


# All estimates 
g <- 
  ggplot(gg_df, aes(x = estimate_100, y = study_name_short)) +
  geom_point(alpha = 0.8, stroke = 0) +
  geom_errorbarh(aes(xmin = conf.low_100, xmax = conf.high_100), height = 0, alpha = 0.5) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_vline(data = summary_df, aes(xintercept = estimate_100)) +
  geom_rect(
    data = summary_df,
    aes(xmin = conf.low_100, xmax = conf.high_100, x = NULL, y = NULL),
    ymin = 0,
    ymax = 100,
    alpha = .1
  ) + 
  geom_text(x = 20, aes(y = study_name_short, label = se_entry), hjust = 1, size = 2, nudge_y = 0.2) +
  coord_cartesian(xlim = c(-20, 20)) +
  xlab("CATE estimate in percentage points") +
  facet_grid(rows = vars(facet), cols = vars(cand_white), scales = "free_y", space = "free") +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.text.y = element_text(size = 8),
        legend.position = "bottom",
        strip.background = element_blank(),
        strip.text.y = element_blank())

g

# ggsave(
#   "meta_cate_cand_race.pdf",
#   g,
#   height = 3.5,
#   width = 6.5
# )
